﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Collections;

namespace LCL.Tools.WinFrm.UserControls
{
    public partial class UcDatatypeMap : UserControl
    {
        private string filename = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(new char[]
		{
			'\\'
		}) + "\\DatatypeMap.cfg";
        public UcDatatypeMap()
        {
            InitializeComponent();
        }
        private void UcDatatypeMap_Load(object sender, EventArgs e)
        {
            try
            {
                InitFrm();
            }
            catch
            {

            }
        }
        private void InitFrm()
        {
            this.comboBox1.DataSource = LoadFromCfg();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string typeName = this.comboBox1.SelectedItem.ToString();
            this.groupBox1.Text = typeName;
            BinListView(typeName, this.listView1);
        }
        private Hashtable GetMapFromCfg(string typeName)
        {
            if (!File.Exists(this.filename))
            {
                return null;
            }
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(this.filename);
            Hashtable hashtable = DatatypeMap.LoadFromCfg(xmlDocument, typeName);
            return hashtable;
        }
        private List<string> LoadFromCfg()
        {
            List<string> list = new List<string>();
            if (!File.Exists(this.filename))
            {
                return list;
            }
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(this.filename);
            XmlNode xmlNode = xmlDocument.SelectSingleNode("Map");
            if (xmlNode != null)
            {
                foreach (XmlNode xmlNode2 in xmlNode.ChildNodes)
                {
                    list.Add(xmlNode2.Name);
                }
            }
            return list;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string text = this.comboBox1.SelectedItem.ToString();
            string key = this.tbKey.Text;
            string value = this.tbValue.Text;

            if (key.Length > 0 && value.Length > 0)
            {
                ListViewItem listViewItem = new ListViewItem(key, 0);
                listViewItem.SubItems.Add(value);
                this.listView1.Items.AddRange(new ListViewItem[] { listViewItem });
            }
        }
        private void MakeXmlDoc(XmlDocument xmldoc, Hashtable list, XmlElement root, ListView listview, string NodeText)
        {
            list = new Hashtable();
            XmlElement xmlElement = xmldoc.CreateElement("", NodeText, "");
            root.AppendChild(xmlElement);
            foreach (ListViewItem listViewItem in listview.Items)
            {
                string text = listViewItem.SubItems[0].Text;
                string text2 = listViewItem.SubItems[1].Text;
                if (!list.Contains(text))
                {
                    XmlElement xmlElement2 = xmldoc.CreateElement("", NodeText, "");
                    XmlAttribute xmlAttribute = xmldoc.CreateAttribute("key");
                    xmlAttribute.Value = text;
                    xmlElement2.Attributes.Append(xmlAttribute);
                    XmlAttribute xmlAttribute2 = xmldoc.CreateAttribute("value");
                    xmlAttribute2.Value = text2;
                    xmlElement2.Attributes.Append(xmlAttribute2);
                    xmlElement.AppendChild(xmlElement2);
                    list.Add(text, text2);
                }
            }
        }
        public void SaveData()
        {
            try
            {
               string selTypeName = this.comboBox1.SelectedItem.ToString();
               List<string> maps=  LoadFromCfg();
               XmlDocument _xmlDocument = new XmlDocument();
               XmlNode newChild = _xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
               _xmlDocument.AppendChild(newChild);
               Hashtable list = null;
               XmlElement xmlElement = _xmlDocument.CreateElement("Map");
               _xmlDocument.AppendChild(xmlElement);
               for (int i = 0; i < maps.Count; i++)
               {
                  string typeName = maps[i];
                  if (selTypeName == typeName)
                  {
                      this.MakeXmlDoc(_xmlDocument, list, xmlElement, this.listView1, typeName);
                  }
                  else 
                  {
                      ListView lv = new ListView();
                      BinListView(typeName, lv);
                      this.MakeXmlDoc(_xmlDocument, list, xmlElement, lv, typeName);
                  }
               }
               _xmlDocument.Save(this.filename);
            }
            catch (Exception ex)
            {
                throw new Exception("Save DatatypeMap file fail:" + ex.Message);
            }
        }
        private void BinListView(string typeName, ListView lv)
        {
            Hashtable hashtable = GetMapFromCfg(typeName);
            #region listView1
            lv.View = View.Details;
            lv.GridLines = true;
            lv.FullRowSelect = true;
            lv.CheckBoxes = false;
            lv.Columns.Clear();
            lv.Items.Clear();
            string text = typeName;
            if (text == "DbToCS")
            {
                lv.Columns.Add("DB类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应C#类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "SQLDbType")
            {
                lv.Columns.Add("类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应SQLDbType类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "OraDbType")
            {
                lv.Columns.Add("类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应OleDbType类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "MySQLDbType")
            {
                lv.Columns.Add("类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应MySqlDbType类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "OleDbDbType")
            {
                lv.Columns.Add("类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应OleDbDbType类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "SQLiteType")
            {
                lv.Columns.Add("类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应SQLiteType类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "AddMark")
            {
                lv.Columns.Add("数据类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("SQL语句中加引号", 160, HorizontalAlignment.Left);
            }
            else if (text == "ValueType")
            {
                lv.Columns.Add("类型", 160, HorizontalAlignment.Left);
                lv.Columns.Add("是否值类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "AccessTypeMap")
            {
                lv.Columns.Add("数字编号", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应类型", 160, HorizontalAlignment.Left);
            }
            else if (text == "ParamePrefix")
            {
                lv.Columns.Add("数据库", 160, HorizontalAlignment.Left);
                lv.Columns.Add("对应参数符号", 160, HorizontalAlignment.Left);
            }
            else if (text == "Translator")
            {
                lv.Columns.Add("英文", 160, HorizontalAlignment.Left);
                lv.Columns.Add("中文", 160, HorizontalAlignment.Left);
            }
            #endregion
            foreach (DictionaryEntry dictionaryEntry in hashtable)
            {
                ListViewItem listViewItem = new ListViewItem(dictionaryEntry.Key.ToString(), 0);
                listViewItem.SubItems.Add(dictionaryEntry.Value.ToString());
                lv.Items.AddRange(new ListViewItem[] { listViewItem });
            }
        }
    }
}
